package com.info.provider.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;

/**
 * Created by pengshuo on 2018/1/10 10:43
 * Description:
 * Modified By:
 * Version:
 *
 * @author pengshuo
 */
public class AESUtil {

    private static Logger logger = LogManager.getLogger(AESUtil.class);

    /**
     * 根据 sessionKey 和 iv 解密 敏感数据
     * @param sessionKey sessionKey
     * @param encryptedData 蜜柑数据
     * @param iv 偏移量
     * @return
     */
    public static String decryptEncryptedData(String sessionKey,String encryptedData,String iv){
        try {
            AES aes = new AES();
            byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
            if(null != resultByte && resultByte.length > 0){
                return new String(resultByte, "UTF-8");
            }
        } catch (InvalidAlgorithmParameterException | UnsupportedEncodingException e) {
            logger.error("根据 sessionKey 和 iv 解密 敏感数据 异常 {}",e);
        }
        return null;
    }
}
